import { ZResponsePageDataResult } from "@/utils/http/response";
import { octpHttp } from "..";
import { PageRequest } from "../models/base";
import { User } from "../models/user";

const url = (path: string) => `/user/${path}`;
const request = octpHttp.request;

export interface RegisterUserParams {
  num: string;
  name: string;
  password: string;
  address?: string;
  phone?: string;
  mail?: string;
  qq?: string;
  wechat?: string;
  description?: string;
}

export const registerUser = (params: RegisterUserParams) =>
  request({
    url: url("register"),
    method: "post",
    params,
  });

export const login = (params: { num: string; password: string }) =>
  request<{ token: string }>({
    url: url("login"),
    method: "post",
    params,
  });

export interface GetUserByIdParams {
  id: number;
}

export const getUserById = (params: GetUserByIdParams) =>
  request<User>({
    url: url("index"),
    method: "get",
    params,
  });

export interface GetUserListParams extends PageRequest {
  name?: string;
}

export const getUserList = (params: GetUserListParams) =>
  request<ZResponsePageDataResult<User>>({
    url: url("list"),
    method: "get",
    params,
  });

export const deleteUserById = (id: number) =>
  request({
    url: url("delete"),
    method: "delete",
    params: { id },
  });

// self

export const getSelfInfo = () =>
  request<User>(
    {
      url: url("self"),
      method: "get",
    },
    { carryToken: true }
  );

export const modifySelfPassword = (params: {
  num: string;
  password: string;
  new_password: string;
}) =>
  request<User>(
    {
      url: url("password"),
      method: "post",
      params,
    },
    { carryToken: true }
  );

export interface UpdateSelfInfoParams {
  name: string;
  num: string;
  address: string;
  phone: string;
  mail: string;
  qq: string;
  wechat: string;
  description: string;
  avatar_id: number;
}

export const updateSelfInfo = (params: Partial<UpdateSelfInfoParams>) =>
  request<User>(
    {
      url: url("update"),
      method: "post",
      params,
    },
    { carryToken: true }
  );
